#!/usr/bin/env python

import threading
import socket


class TCPServer(socket.socket):
	def __init__(self, *args, **kwargs):
		"""Initialize, and let the user set any Frame settings"""
		socket.socket.__init__(self, *args)
		self.myname=kwargs['name']
		self.bind((kwargs['host'], kwargs['port']))
		Speech.PrintText('waiting for connection')
		self.listen(1)
		self.conn, self.addr = self.accept()
		Speech.PrintText('Client connected: '+str(self.addr))
		while 1:
			data = self.conn.recv(1024)
			if data.lower()=='get_name':
				self.conn.send(myname)

			elif not data: break
			else:
				Speech.PrintText('Client said: '+str(data))
				self.conn.send('You said: '+str(data))
		self.conn.close()		
        
class RobotServer(threading.Thread):
	def __init__(self,*args,**kwargs):
		threading.Thread.__init__(self)
		self.myname='Robbie-the-robot'
		self.HOST=kwargs['host']
		self.PORT=kwargs['port']
		
	def run(self):
		
		self.server=TCPServer(socket.AF_INET, socket.SOCK_STREAM,host=self.HOST,port=self.PORT,name=self.myname)
		
	def SetName(self,name):
		self.name=name
		
	def GetName(self):
		return self.name

class SpeechClass:
	def __init__(self,*args,**kwargs):
		pass
	def PrintText(self,text):
		print text
		

myname='Robbie_the_robot'
print myname + ' is up and running!'
HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
global Speech
Speech=SpeechClass()

Server=RobotServer(host=HOST,port=PORT)
Server.start()
#Server.run()

print 'Testing threading...OK!'

